import { Component, OnInit } from '@angular/core';
import {OneDialogService} from '../../../widgets/one-dialog/one-dialog.service';
import {MsgConstants} from '../../../constants/constants';
import {DaAPI} from '../da-constants';
import {ActivatedRoute, Router} from '@angular/router';
import {TokenUtil} from '../../../util/token-util';
import {PageSubject} from '../../../util/page-subject';
import {DaVocsAuditService} from './da-vocs-audit.service';
import {ResponseUtil} from '../../../util/response-util';

declare var Dropzone;

@Component({
  selector: 'app-da-vocs-audit',
  templateUrl: './da-vocs-audit.component.html',
  styleUrls: ['./da-vocs-audit.component.css']
})
export class DaVocsAuditComponent implements OnInit {
  myDropzone: any;
  siteCode = '';
  siteName = '';
  siteArr: Array<any> = [];
  deviceCode = '';
  deviceArr: Array<any> = [];
  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private dialogService: OneDialogService,
    private pageSubject: PageSubject,
    private tokenUtil: TokenUtil,
    private responseUtil: ResponseUtil,
    private vocAuditService: DaVocsAuditService
  ) { }

  ngOnInit() {
    this.initDropzone();
    this.pushPage();
    this.getSiteData();
  }
  pushPage() {
    // const path = new Path('数据矫正', '');
    this.pageSubject.pushPage('VOCs数据导入', '数据上传');
  }
  initDropzone() {
    this.myDropzone = new Dropzone('#myId', {
      url: DaAPI.daVocFileUpload + '?siteCode=' + this.siteCode + '&siteName=' + this.siteName + '&deviceCode=' + this.deviceCode,
      headers: {
        Authorization:  'Bearer ' + this.tokenUtil.getToken()
      },
      // maxFiles: 1,
      acceptedFiles: '.xls,.xlsx',
      autoProcessQueue: false,
      dictDefaultMessage: '拖动文件至此或者点击上传',
      dictMaxFilesExceeded: '您最多只能上传1个文件！',
      dictResponseError: '文件上传失败!',
      dictInvalidFileType: '文件类型只能是.xls,.xlsx格式文件',
      dictFallbackMessage: '浏览器不受支持',
      dictFileTooBig: '文件过大上传文件最大支持.',
      dictRemoveLinks: '删除',
      dictCancelUpload: '取消',
    });
    this.myDropzone.on('addedfile', () => {
      const queuedFiles = this.myDropzone.getQueuedFiles();
      if (queuedFiles.length > 0) {
        this.myDropzone.removeFile(queuedFiles[0]);
      }
    });
    this.myDropzone.on('success', (file , result) => {
      if (result && result.code === MsgConstants.MSG_CODE_OK) {
        if (result.code === MsgConstants.MSG_CODE_OK) {
          this.dialogService.success('文件：' + file.name + '上传成功');
          this.myDropzone.removeAllFiles();
          // this.cancelSubmit();
        } else {
          this.dialogService.info(result.message);
        }
      }
      if (result && result.code !== MsgConstants.MSG_CODE_OK) {
        this.dialogService.info(result.message);
      }
    });
  }
  // cancelSubmit() {
  //   this.myDropzone.removeAllFiles();
  //   this.router.navigate(['..'], {relativeTo: this.route})
  // }
  submit() {
    if (this.siteCode && this.deviceCode) {
      this.myDropzone.options.url =  DaAPI.daVocFileUpload + '?siteCode=' + this.siteCode + '&siteName=' + this.siteName + '&deviceCode=' + this.deviceCode;
      this.myDropzone.processFiles(this.myDropzone.getQueuedFiles());
    } else {
      if (!this.siteCode) {
        this.dialogService.info('请选择站点');
      }
    }
  }
  /**
   * 获取站点数据
   */
  getSiteData = () => {
    this.vocAuditService.getSiteData().subscribe(
      result => this.responseUtil.success(result, this.progressD),
      error => this.responseUtil.error(error)
    )
  };
  progressD = (result) => {
    if (result && result.code === MsgConstants.MSG_CODE_OK) {
      if (result.data && result.data.length > 0) {
        this.siteArr = result.data;
        this.siteCode = this.siteArr[0].SITECODE;
        this.siteName = this.siteArr[0].SITENAME;
        this.getDeviceData();
      }
    } else {
      this.dialogService.info(result.message);
    }
  };
  /**
   * 获取站点数据
   */
  getDeviceData = () => {
    this.siteArr.every((val, i) => {
      if (val.SITEODE === this.siteCode) {
        this.siteName = val.SITENAME;
        return false;
      } else {
        return true;
      }
    });
    this.vocAuditService.getDeviceData(this.siteCode).subscribe(
      result => this.responseUtil.success(result, this.progressDeviceData),
      error => this.responseUtil.error(error)
    )
  };
  progressDeviceData = (result) => {
    if (result && result.code === MsgConstants.MSG_CODE_OK) {
      if (result.data && result.data.length > 0) {
        this.deviceArr = result.data;
        this.deviceCode = this.deviceArr[0].SNCODE;
      }
    } else {
      this.dialogService.info(result.message);
    }
  };
}
